import React, { createContext, useReducer } from 'react';



//获取二级路由的信息
const authReducer = (state, action) => {

    if (!action.type && action.categories) {

        return {
            ...state,
            categories: action.categories,
        }
    }
    if (!action.type && action.category_id >= 0) {

        return {
            ...state,
            category_id: action.category_id,
        }
    }
    if (!action.type && action.articles) {
        return {
            ...state,
            articles: action.articles,
        }
    }
    if (!action.type && action.moreArticles) {
        // 将合并文章列表放在这里处理，可以解决别dispatch 的影响
        return {
            ...state,
            articles: [...state.articles, ...action.moreArticles],
        }
    }
    // switch 在这里用处不大
    switch (action.type) {
        case "后端":
            return {
                ...state,
                category_id: action.category_id,
            }
        case "Android":
            return {
                ...state,
                category_id: action.category_id,
            }
        case "前端":
            return {
                ...state,
                category_id: action.category_id,
            }

        case "iOS":
            return {
                ...state,
                category_id: action.category_id,
            }
        case "推荐":

            return {
                ...state,
                category_id: action.category_id,
            }
        default:

            return state
    }
}

export const initState = {
    categories: [

    ],
}
export const AuthContext = createContext(initState)

export const ArtContext = (props) => {

    const [state, dispatch] = useReducer(authReducer, initState)
    return (
        <AuthContext.Provider value={{
            state,
            dispatch
        }}>
            {props.children}
        </AuthContext.Provider>
    )
}